package com.andong.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class CommonUtils {
    public static Integer getGender(String idNumber) {
        int flag = Integer.parseInt(idNumber.charAt(16) + "");
        if (flag % 2 == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    public static Integer getAge(String idNumber) {
        String birthStr = idNumber.substring(6, 14);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate birthday = LocalDate.parse(birthStr, dateTimeFormatter);
        LocalDate date2 = LocalDate.now();
        return (Integer) (int) ChronoUnit.YEARS.between(birthday, date2);
    }

    public static boolean checkAgeAndType(Integer age, Integer type) {
        if (age < 18) { // 小于18岁不允许申请所有驾照
            return false;
        } else if ((age < 20 || age > 60) && (type == 2 || type == 3)) {
            // 小于20岁或者大于60岁不允许申请B1和B2
            return false;
        } else return (age >= 22 && age <= 60) || type != 1;
    }

    public static String getDefaultPassword(String idNumber) {
        return idNumber.substring(12);
    }

    public static boolean isDelete(Integer score, Integer times, Integer subject, Integer licenseType) {
        return times == 5 && ((subject == 2 && (licenseType < 4 ? score < 90 : score < 80)) || (subject == 3 && score < 90));
    }

    public static boolean isNextStage(Integer score, Integer subject, Integer licenseType) {
        return (subject == 1 || subject == 4 || subject == 3) && score >= 90
                || (subject == 2 && (licenseType >= 4 ? score >= 80 : score >= 90));
    }
}
